package com.pur.zsms.matermanage.mater.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.matermanage.mater.dao.IMaterDao;
import com.pur.zsms.matermanage.mater.dao.IMaterUnitDao;
import com.pur.zsms.matermanage.mater.po.CheckMaterCostPo;
import com.pur.zsms.matermanage.mater.po.CheckMaterSalePo;
import com.pur.zsms.matermanage.mater.po.UnitMaterConvertPo;
import com.pur.zsms.matermanage.mater.service.IMaterCheckService;
import com.pur.zsms.supplier.dao.SupplierDao;
import com.pur.zsms.systemsetting.datadictionary.dao.IDataDictionaryDao;

@Service("materCheckService")
public class MaterCheckServiceImpl implements IMaterCheckService {

	String msg = "";

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Resource(name = "dataDictionaryDao")
	private IDataDictionaryDao dataDictionaryDao;

	@Resource(name = "materDao")
	private IMaterDao materDaoImpl;

	@Resource(name = "materUnitDao")
	private IMaterUnitDao materUnitDaoImpl;

	@Resource(name = "supplierDao")
	private SupplierDao supplierDao;

	public boolean checkMaterCost(int supId, int materId, String materValue) throws GenericException {

		List<CheckMaterCostPo> tCheckMaterCostPoList = materDaoImpl.getCheckMaterCostPo(supId, materId);

		if (tCheckMaterCostPoList != null && tCheckMaterCostPoList.size() > 0) {
			String materName = materDaoImpl.getMaterPoByMaterId(materId).get(0).getMaterName();

			CheckMaterCostPo tCheckMaterCostPo = tCheckMaterCostPoList.get(0);
			if (new BigDecimal(materValue).compareTo(new BigDecimal(tCheckMaterCostPo.getLowLimit())) < 0) {
				msg = materName + "进货价格小于设定值(" + tCheckMaterCostPo.getLowLimit() + "￥" + ")预警";
				return false;
			} else if (new BigDecimal(materValue).compareTo(new BigDecimal(tCheckMaterCostPo.getUpperLimit())) > 0) {
				msg = materName + "进货价格大于设定值(" + tCheckMaterCostPo.getUpperLimit() + "￥" + ")预警";
				return false;
			} else {
				return true;
			}
		} else {
			return true;

		}
	}

	public boolean checkMaterSale(int supId, int materId, String materValue) throws GenericException {
		List<CheckMaterSalePo> tCheckMaterSalePoList = materDaoImpl.getCheckMaterSalePo(supId, materId);

		if (tCheckMaterSalePoList != null && tCheckMaterSalePoList.size() > 0) {
			String materName = materDaoImpl.getMaterPoByMaterId(materId).get(0).getMaterName();
			CheckMaterSalePo tCheckMaterSalePo = tCheckMaterSalePoList.get(0);
			if (new BigDecimal(materValue).compareTo(new BigDecimal(tCheckMaterSalePo.getLowLimitSale())) < 0) {
				msg = materName + "销售价格小于设定值(" + tCheckMaterSalePo.getLowLimitSale() + "￥" + ")预警";
				return false;
			} else if (new BigDecimal(materValue).compareTo(new BigDecimal(tCheckMaterSalePo.getUpperLimitSale())) > 0) {
				msg = materName + "销售价格大于设定值(" + tCheckMaterSalePo.getUpperLimitSale() + "￥" + ")预警";
				return false;
			} else {
				return true;
			}
		} else {
			return true;

		}
	}

	public boolean checkMaterUnitChange(int supId, int materId) throws GenericException {
		List<UnitMaterConvertPo> tUnitMaterConvertPo = materUnitDaoImpl.getUnitMaterConvertPoByAuxiliaryMaterId(materId, supId);
		if (tUnitMaterConvertPo != null && tUnitMaterConvertPo.size() > 0) {
			String materName = materDaoImpl.getMaterPoByMaterId(materId).get(0).getMaterName();
			msg = materId + ":" + materName + "需要进行单位转换！";
			return false;
		} else {
			return true;
		}
	}

	public UnitMaterConvertPo checkHostMaterUnit(int supId, int materId) throws GenericException {
		List<UnitMaterConvertPo> tUnitMaterConvertPoList = materDaoImpl.getUnitMaterConvertPo(supId, materId);
		if (tUnitMaterConvertPoList != null && tUnitMaterConvertPoList.size() == 1) {
			return tUnitMaterConvertPoList.get(0);
		} else {
			return null;
		}
	}

	public UnitMaterConvertPo getUnitMaterConvertPoByAuxiliaryMaterId(int supId, int auxiliaryMaterId) throws GenericException {
		return materDaoImpl.getUnitMaterConvertPo(supId, auxiliaryMaterId).get(0);
	}

}
